package com.k.util.excel;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.util.stream.IntStream;

public class POIUtilTest {
    public static void main(String[] args) {
        String type = "xlsx";
        // test XSSFWorkbook
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        testXSSFWorkbook(xssfWorkbook);
        String pathName2 = "E:/test/excelTest20241130_1.xlsx";
        ExcelUtils.export(xssfWorkbook, pathName2);

        // test HSSFWorkbook
//        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//        testHSSFWorkbook(hssfWorkbook);
    }

    private static void testHSSFWorkbook(Workbook workbook) {
        HSSFSheet sheet = (HSSFSheet) workbook.createSheet();

        // 线段格子:row0 col0
        sheet.setColumnWidth(0, 2048 * 4);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
        POIHSSFUtil.createContent(workbook, sheet, 0, 0, "班次      人员                   勤务内容");
        int x1 = 70, y1 = 77;
        int x3 = 144, y3 = 65;
        int[] xy2 = {x1, y1, x3, y3};
        POIHSSFUtil.drawLine(sheet, null, 0, 0, 0, 1, 144, 77, xy2);

        // title: 勤务内容: row0 colN row1 colN
        for (int i = 0; i < 2; i++) {
            Row row = sheet.getRow(i) == null ? sheet.createRow(i) : sheet.getRow(i);
            row.setHeight((short) (255 * 2));
            for (int j = 0; j < 5; j++) {
                int titleColIdx = j + 1;
                sheet.setColumnWidth(titleColIdx, 2048 * 4);
                POIHSSFUtil.createContent(workbook, sheet, i, titleColIdx, "content_" + i + "_" + titleColIdx);
            }
        }

        // 勤务项目合并
        POIHSSFUtil.merge(sheet, 0, 0, 1, 2);
        POIHSSFUtil.createContent(workbook, sheet, 0, 1, "merger1");
        POIHSSFUtil.merge(sheet, 0, 0, 3, 5);
        POIHSSFUtil.createContent(workbook, sheet, 0, 3, "merger2");
    }

    private static void testXSSFWorkbook(XSSFWorkbook workbook) {
        XSSFSheet sheet = workbook.createSheet();
        IntStream.range(0, 5).forEach(sheet::createRow);

        System.out.println("default column width:" + sheet.getColumnWidth(0));
        System.out.println("default row height:" + sheet.getRow(0).getHeight());
        String title1 = "总标题";
        String titleInLine = POIXSSFUtil.getKong(50) + "勤务内容 \n\n " + POIXSSFUtil.getKong(10) + " 班次 " + POIXSSFUtil.getKong(40) + "人员 ";

        // 线段格子:row0 col0
        sheet.setColumnWidth(0, 2048 * 4);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
        POIXSSFUtil.createContent(workbook, sheet, 0, 0, titleInLine, POIXSSFUtil.getLineCellStyle(workbook));

        // 所划线的区域设置 划线区域的 宽度  高度
        int drawAreaMinColIdx = 0;
        int drawAreaMinRowIdx = 0;
        int drawAreaMaxColIdx = 0;
        int drawAreaMaxRowIdx = 1;
        int drawAreaWidth = sheet.getColumnWidth(0);
        int drawAreaHeight = sheet.getRow(0).getHeight() + sheet.getRow(1).getHeight();

        // ===================
        // 方法 一
        // ===================
        XSSFDrawing xssfDrawing = sheet.createDrawingPatriarch();
        int widthX = drawAreaWidth * 300;
        int heightX = drawAreaHeight * 700;
        // 对角线 一
        // 起点(row0col0 左上顶点)  --  终点(row1col2 左上顶点)
        XSSFClientAnchor a = new XSSFClientAnchor(0, 0, 0, 0, (short) 0, 0, (short) 1, 2);
        XSSFSimpleShape shape = xssfDrawing.createSimpleShape(a);
        shape.setShapeType(ShapeTypes.LINE);
        shape.setLineWidth(0.5);
        shape.setLineStyle(0);
        shape.setLineStyleColor(0, 0, 0);

        // 对角线 二
        XSSFClientAnchor a1 = new XSSFClientAnchor(0, 0, widthX, heightX, (short) 0, 0, (short) 0, 1);
        XSSFSimpleShape shape1 = xssfDrawing.createSimpleShape(a1);
        shape1.setShapeType(ShapeTypes.LINE);
        shape1.setLineWidth(0.5);
        shape1.setLineStyle(0);
        shape1.setLineStyleColor(0, 0, 0);

        // 起点(row0col0 左上顶点)  --  终点row1col0 偏移 只需要修改 width:dx2
        XSSFClientAnchor a2 = new XSSFClientAnchor(0, 0, widthX / 2, heightX, (short) 0, 0, (short) 0, 1);
        XSSFSimpleShape shape2 = xssfDrawing.createSimpleShape(a2);
        shape2.setShapeType(ShapeTypes.LINE);
        shape2.setLineWidth(0.5);
        shape2.setLineStyle(0);
        shape2.setLineStyleColor(0, 0, 0);
//
        // 只需要修改 dy2:height
        XSSFClientAnchor a3 = new XSSFClientAnchor(0, 0, widthX, 0, (short) 0, 0, (short) 0, 1);
        XSSFSimpleShape shape3 = xssfDrawing.createSimpleShape(a3);
        shape3.setShapeType(ShapeTypes.LINE);
        shape3.setLineWidth(0.5);
        shape3.setLineStyle(0);
        shape3.setLineStyleColor(0, 0, 0);


        // ===================
        // 方法 二
        // ===================
        drawAreaMinColIdx = 0;
        drawAreaMinRowIdx = 2;
        drawAreaMaxColIdx = 0;
        drawAreaMaxRowIdx = 3;

        drawAreaWidth = sheet.getColumnWidth(0);
        drawAreaHeight = sheet.getRow(2).getHeight() * 2;
        POIXSSFUtil.drawLine(sheet, drawAreaWidth, drawAreaHeight,
                drawAreaMinColIdx, drawAreaMinRowIdx, drawAreaMaxColIdx, drawAreaMaxRowIdx,
                0,
                0,
                50,
                100);
        POIXSSFUtil.drawLine(sheet, drawAreaWidth, drawAreaHeight,
                drawAreaMinColIdx, drawAreaMinRowIdx, drawAreaMaxColIdx, drawAreaMaxRowIdx,
                0,
                0,
                100,
                20);
        // title: 勤务内容: row0 colN row1 colN
        for (int i = 0; i < 2; i++) {
            Row row = sheet.getRow(i) == null ? sheet.createRow(i) : sheet.getRow(i);
            row.setHeight((short) (255 * 2));
            for (int j = 0; j < 5; j++) {
                int titleColIdx = j + 1;
                POIXSSFUtil.createContent(workbook, sheet, i, titleColIdx, "content_" + i + "_" + titleColIdx, POIXSSFUtil.getTitleStyle(workbook));
                sheet.setColumnWidth(titleColIdx, 2048 * 4);
            }
        }
        // 勤务项目合并
        POIXSSFUtil.merge(sheet, 0, 0, 1, 2);
        POIXSSFUtil.createContent(workbook, sheet, 0, 1, "merger1", POIXSSFUtil.getTitleStyle(workbook));
        POIXSSFUtil.merge(sheet, 0, 0, 3, 5);
        POIXSSFUtil.createContent(workbook, sheet, 0, 3, "merger2", POIXSSFUtil.getTitleStyle(workbook));
    }
}
